package cn.ac.jaas.ljx.wordsegmentation.task;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.ac.jaas.ljx.wordsegmentation.domain.Document;
import cn.ac.jaas.ljx.wordsegmentation.domain.Word;
import cn.ac.jaas.ljx.wordsegmentation.interfaces.Segmenter;
import cn.ac.jaas.ljx.wordsegmentation.interfaces.impl.AnsjSegmenter;
import cn.ac.jaas.ljx.wordsegmentation.interfaces.impl.NLPIRSegmenter;
import cn.ac.jaas.ljx.wordsegmentation.interfaces.impl.WordSegmenter;
import cn.ac.jaas.ljx.wordsegmentation.util.DocumentReadUtil;
import cn.ac.jaas.ljx.wordsegmentation.util.DocumentWriteUtil;
import cn.ac.jaas.ljx.wordsegmentation.util.WordCountUtil;
//import org.springframework.beans.factory.annotation.Value;

public class SegmenterTask {
	
	//@Value("${document.dir}")
	private static final String DOCUMENT_DIR = "D:/segment/document.xls";
	//@Value("${result.dir}")
	private static final String RESULT_DIR = "D:/segment/";
	private static List<Document> documents;
	static {
		System.out.println("*****************Docuemnt Read*****************");
		documents = DocumentReadUtil.excelRead(DOCUMENT_DIR);
		System.out.println("*****************Read End*****************");
	}
	
	private static Segmenter segmenter;
	
	public static void doTask(String segmenterName) {
		switch(segmenterName) {
		case "Ansj" :
			segmenter = new AnsjSegmenter();
			break;
		case "NLPIR" :
			segmenter = new NLPIRSegmenter();
			break;
		case "Word" :
			segmenter = new WordSegmenter();
			break;
		default :
			System.err.println("no such of segmenter named" + segmenterName);
			return;
		}
		List<Word> wordsCounted = new ArrayList<>();
		int i=1;
		for(Document document : documents) {
			List<Word> wordsNoCount = new ArrayList<>();
			wordsNoCount = segmenter.segment(document);
			if(wordsNoCount != null) {
				WordCountUtil.wordCount(wordsNoCount, wordsCounted);
			}
			System.out.println(i++ + "done");
		}
		File file = new File(RESULT_DIR + segmenterName + "_result.xls");
		System.out.println("*****************Result Write*****************");
		DocumentWriteUtil.excelWriteResult(file, wordsCounted);
		System.out.println("*****************Write End*****************");
	}
}
